package com.motong.gongdan.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Auther: FreddieFang
 * @Date: 2022/02/09 15:31
 * @Description:
 */
@Data
public class ResponseData<T> {
    private String message;//错误 异常 成功等提示信息

    private Integer code;//

    private T data;//成功返回的数据

    private boolean more;

    // 数据总数
    @ApiModelProperty(notes = "数据总数")
    private Long count;

    public ResponseData(String message, Integer code, T data) {
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public ResponseData(String message, Integer code, T data, Long count) {
        this.message = message;
        this.code = code;
        this.data = data;
        this.count = count;
    }

    public ResponseData() {
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getStandardMessage();
    }

    public static <T> ResponseData<T> success(T data) {
        return new ResponseData<T>(Status.SUCCESS.getStandardMessage(), Status.SUCCESS.getCode(), data);
    }

    public static <T> ResponseData<T> success(T data, Long count) {
        return new ResponseData<T>(Status.SUCCESS.getStandardMessage(), Status.SUCCESS.getCode(), data, count);
    }

    public static <T> ResponseData<T> success(String message, T data) {
        return new ResponseData<T>(message, Status.SUCCESS.getCode(), data);
    }

    public static <T> ResponseData<T> successOther(Integer code, T data) {
        return new ResponseData<T>(Status.SUCCESS.getStandardMessage(), code, data);
    }


    public static <T> ResponseData<T> fail(Integer code, String message) {
        return new ResponseData<T>(message, code, null);
    }

    public static <T> ResponseData<T> fail(String message) {
        return new ResponseData<T>(message, Status.INTERNAL_SERVER_ERROR.getCode(), null);
    }


    public enum Status {
        SUCCESS(200, "ok"),
        BAD_REQUEST(400, "Bad Request"),
        NOT_FOUND(404, "Not Found"),
        INTERNAL_SERVER_ERROR(500, "Unkonw Internal Error"),
        NOT_VALID_PARAM(40005, "Not valid Params"),
        NOT_SUPPORTED_OPERATION(40006, "Operation not supported"),
        NOT_LOGIN(50000, "Not Login");
        private int code;
        private String standardMessage;

        Status(int code, String standardMessage) {
            this.code = code;
            this.standardMessage = standardMessage;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getStandardMessage() {
            return standardMessage;
        }

        public void setStandardMessage(String standardMessage) {
            this.standardMessage = standardMessage;
        }
    }

}
